﻿// var authority = [1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理
// authority[6] = 1;
/**
 * 5:客户查看报告
 * 6.业务发布报告
 * 7.检测主管制定报告
 * 8.审核人员审核报告
 * 18.超级管理员批准报告
 */
if ((authority[5] | authority[6] | authority[7] | authority[8]) === 0) {
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}
var chosenWorkpiece = []; //所有选中的委托单，详见OnCheckboxChange
var rawData = [[], [], []];
//发送请求获取列表
console.log(authority);
//超级管理员批准报告
if (authority[18] === 1){
    $.ajax({
        url: '/echo/report_super_approve',
        type: 'post',
        data: {
            "requestType": "list"
        },
        async: false,
        success: function (data, status) {
            if (status == 'success') {
                console.log(data);
                for (let i = 0; i < data.length; i++) {
                    let state;
                    switch (data[i].r_state) {
                        case 2: {
                            state = '未审批'; break;
                        }
                        case 3: {
                            state = "审批不通过"; break;
                        }
                        case 4: {
                            state = "审批通过"; break;
                        }
                    }
                    if(data[i].r_date!=null){
                        var date1 = new Date(data[i].r_date);
                        data[i].r_date =  date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
                        date1.getDate();
                    }
                    let temp = [
                        data[i].r_id,
                        data[i].r_pname,
                        data[i].r_product,
                        data[i].r_date == null ? "" : data[i].r_date,
                        state,
                    ];
                    console.log(rawData[2])
                    rawData[2].push(temp);
                    data[i].r_state == 2 ? rawData[0].push(temp) : rawData[1].push(temp);
                }
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
} else {
    //客户查看自己的报告
    if (authority[5] === 1) {
        $.ajax({
            url: '/echo/report_client',
            type: 'post',
            data: {
                "user_id": user.user_id
            },
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    console.log(data);
                    for (let i = 0; i < data.length; i++) {
                        var date = new Date(data[i].r_date);
                        data[i].r_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                        date.getDate();
                        let temp = [
                            data[i].r_id,
                            data[i].r_pname,
                            data[i].r_product,
                            data[i].r_date == null ? "" : data[i].r_date,
                            data[i].r_remark
                        ];
                        rawData[2].push(temp);
                    }
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
    }
    //业务向用户发布报告
    else if (authority[6] === 1) {
        $.ajax({
            url: '/echo/report_public',
            type: 'post',
            data: {
                "requestType": "list"
            },
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    console.log(data);
                    for (let i = 0; i < data.length; i++) {
                        let state,flag=0;
                        switch (data[i].r_state) {

                            case 4: {
                                state = "未发送"; break;
                            }
                            case 5: {
                                state = "已发送";
                                flag = 1;
                                break;
                            }


                        }
                        if(data[i].r_date!=null){
                            var date1 = new Date(data[i].r_date);
                            data[i].r_date =  date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
                            date1.getDate();
                        }
                        if(data[i].r_verify_date!=null){
                            var date2 = new Date(data[i].r_verify_date);
                            data[i].r_verify_date = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
                            date2.getDate();
                        }
                        let temp = [
                            data[i].r_id,
                            data[i].r_pname,
                            data[i].r_id,
                            data[i].r_product,
                            data[i].r_company,
                            data[i].user_name,
                            data[i].r_date == null ? "" : data[i].r_date,
                            state,
                            data[i].r_verify_date == null ? "" : data[i].r_verify_date,
                            flag,
                        ];
                        rawData[2].push(temp);
                        if (data[i].r_state == 4) {
                            rawData[0].push(temp);
                        }
                        else {
                            rawData[1].push(temp);
                        }
                    }
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
    }
    //7 检测主管选择工件生成报告
    else if (authority[7] === 1) {
        $.ajax({
            url: '/echo/report',
            type: 'post',
            data: {
                "requestType": "worklist",
                "user_part": user.staff_part
            },
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    console.log(data)
                    for (let i = 0; i < data.length; i++) {
                        if(data[i].d_fin!=null){
                            var date2 = new Date(data[i].d_fin);
                            data[i].d_fin = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
                            date2.getDate();
                        }
                        rawData[0].push([
                            data[i].task_pid,
                            data[i].task_pname,
                            data[i].task_product,//产品名称
                            data[i].task_pdid,
                            data[i].w_pipeName,
                            data[i].w_pipeNum,
                            data[i].w_spec,
                            data[i].task_company,
                            data[i].w_test,
                            data[i].w_qua,
                            data[i].w_scale,
                            data[i].w_texture,
                            data[i].w_weldM,
                            data[i].w_weldPer,
                            data[i].d_fin == null ? "" : data[i].d_fin,
                            data[i].w_tid,
                            data[i].op_id,
                            data[i].op_techid,
                            data[i].w_id
                        ]);
                    }
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
        $.ajax({
            url: '/echo/report_list',
            type: 'post',
            data: {
                "user_part": user.staff_part
            },
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    for (let i = 0; i < data.length; i++) {
                        let state;
                        switch (data[i].r_state) {
                            case 0: {
                                state = "未审核"; break;
                            }
                            case 1: {
                                state = "审核不通过"; break;
                            }
                            case 2: {
                                state = "审核通过"; break;
                            }
                            case 3: {
                                state = "批准不通过"; break;
                            }
                            case 4: {
                                state = "批准通过"; break;
                            }
                        }
                        if(data[i].r_date!=null){
                            var date2 = new Date(data[i].r_date);
                            data[i].r_date = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
                            date2.getDate();
                        }
                        if(data[i].r_verify_date!=null){
                            var date1 = new Date(data[i].r_verify_date);
                            data[i].r_verify_date = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
                            date1.getDate();
                        }
                        rawData[1].push([
                            data[i].r_id,
                            data[i].r_pid,
                            data[i].r_pname,
                            data[i].r_company,
                            data[i].r_date == null ? "" : data[i].r_date,
                            state,
                            data[i].r_verify_date == null ? "" : data[i].r_verify_date,
                            data[i].r_remark
                        ]);
                    }
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
    }
    //审核人员审核报告
    else if (authority[8] === 1) {
        $.ajax({
            url: '/echo/report_check',
            type: 'post',
            data: {
                "requestType": "list"
            },
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    for (let i = 0; i < data.length; i++) {
                        let state;
                        switch (data[i].r_state) {
                            case 0: {
                                state = "未审核"; break;
                            }
                            case 1: {
                                state = "审核不通过"; break;
                            }
                            case 2: {
                                state = "审核通过"; break;
                            }
                            case 3: {
                                state = "批准不通过"; break;
                            }
                            case 4: {
                                state = "批准通过"; break;
                            }
                        }
                        if(data[i].r_date!=null){
                            var date2 = new Date(data[i].r_date);
                            data[i].r_date = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
                            date2.getDate();
                        }
                        if(data[i].r_verify_date!=null){
                            var date1 = new Date(data[i].r_verify_date);
                            data[i].r_verify_date = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
                            date1.getDate();
                        }
                        let temp = [
                            data[i].r_id,
                            data[i].r_pid,
                            data[i].r_pname,
                            data[i].r_company,
                            data[i].r_date == null ? "" : data[i].r_date,
                            state,
                            data[i].r_verify_date == null ? "" : data[i].r_verify_date,
                            data[i].r_remark
                        ];
                        rawData[2].push(temp);
                        if (data[i].r_state == 0) {
                            rawData[0].push(temp);
                        }
                        else {
                            rawData[1].push(temp);
                        }
                    }
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });

    }
}

var dummyData = [].concat(rawData);
var title;
var methodCount;
var neededOptionIndex;
//显示选项卡
var optionCard = document.getElementById("optionCard");
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
//超级管理员
if (authority[18] === 1){
    // optionCard.parentElement.removeChild(optionCard);
    optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lA\" data-toggle=\"tab\">未审批</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已审批</a></li>";
    title = [
        ["编号", "项目名称", "产品名称", "报告生成时间", "状态", "操作"],
        ["编号", "项目名称", "产品名称", "报告生成时间", "状态", "操作"],
        ["编号", "项目名称", "产品名称", "报告生成时间", "状态", "操作"]];
    methodCount = [5, 5, 5];
    neededOptionIndex = [
        [1, 2],
        [1, 2],
        [1, 2]//页面上只有这一栏
    ];
}else {
    //客户
    if (authority[5] === 1) {//客户查看报告
        optionCard.parentElement.removeChild(optionCard);
        title = [
            ["编号", "", "项目名称", "", "报告生成时间", "", "", "备注", "操作"],
            ["编号", "", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "备注", "操作"],
            ["编号", "项目名称", "产品名称", "报告生成时间","备注", "操作"]];
        methodCount = [8, 8, 5];
        neededOptionIndex = [
            [],
            [],
            [1, 2]//页面上只有这一栏
        ];
    }
    else if (authority[6] === 1) {//业务发布报告
        optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
            "<li><a href=\"#lA\" data-toggle=\"tab\">未发送</a></li>" +
            "<li><a href=\"#lB\" data-toggle=\"tab\">已发送</a></li>";
        title = [
            ["编号",  "项目名称","报告编号","产品名称","委托单位","委托人","报告生成时间", "状态", "报告批准时间", "操作","发送"],
            ["编号",  "项目名称","报告编号","产品名称","委托单位","委托人","报告生成时间", "状态", "报告批准时间", "操作","  "],
            ["编号",  "项目名称","报告编号","产品名称","委托单位","委托人","报告生成时间", "状态", "报告批准时间", "操作","发送"]];
        methodCount = [10, 10, 9];
        neededOptionIndex = [
            [1, 3, 4, 5],
            [1, 3, 4, 5],
            [1, 3, 4, 5]
        ];
    }
    else if (authority[7] === 1) {//检测主管制定报告
        optionCard.innerHTML = "<li class=\"active\"><a href=\"#lA\" data-toggle=\"tab\">生成报告</a></li>" +
            "<li><a href=\"#lB\" data-toggle=\"tab\">历史报告</a></li>";
        document.getElementById("lC").setAttribute("class", "tab-pane");
        document.getElementById("lA").setAttribute("class", "tab-pane active");
        title = [
            ["项目编号", "项目名称", "产品名称", "产品编号", "部件/管线名称", "部件/管线编号", "工件规格",
                "委托单位", "检测方法", "合格级别", "检测比例", "材质", "焊接方法", "焊工号", "检测时间", "选择"],
            ["编号", "项目编号", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "备注", "操作"],
            ["编号", "派工单编号", "委托单类型", "工件数量", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "操作"]];
        methodCount = [15, 8, 9];
        neededOptionIndex = [
            [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14],//生成报告
            [2, 3, 5],//历史报告
            []
        ];
    }
    else if (authority[8] === 1) {//审核人员审核报告
        optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
            "<li><a href=\"#lA\" data-toggle=\"tab\">未审核</a></li>" +
            "<li><a href=\"#lB\" data-toggle=\"tab\">已审核</a></li>";
        title = [
            ["编号", "项目编号", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "备注", "操作"],
            ["编号", "项目编号", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "备注", "操作"],
            ["编号", "项目编号", "项目名称", "委托单位", "报告生成时间", "状态", "报告审核时间", "备注", "操作"]];
        methodCount = [8, 8, 8];
        neededOptionIndex = [
            [2, 3],
            [2, 3],
            [2, 3]
        ];
    }

}


var pageID = [1, 1, 1];
//设置每一页的行数
var countPerPage=[];

//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})
var pageButtonCount = [8, 8, 8];
var pageCount = [parseInt((dummyData[0].length - 1) / countPerPage[0]) + 1,
parseInt((dummyData[1].length - 1) / countPerPage[1]) + 1,
parseInt((dummyData[2].length - 1) / countPerPage[2]) + 1];
var table = [document.getElementById("table0"), document.getElementById("table1"), document.getElementById("table2")];
var tbody = [table[0].children[1], table[1].children[1], table[2].children[1]];
var pages = [document.getElementById("pages0"), document.getElementById("pages1"), document.getElementById("pages2")];
let div = document.createElement("div");
div.setAttribute("style", "text-align: center");
// if(authority[8] != 1){
// div.innerHTML = "<button class='btn btn-xs btn-success' onclick='OnCreateReport();'>生成报告</button>";
// }
document.getElementById("lA").appendChild(div);
if(authority[7] ===1){
    div.innerHTML = "<button class='btn btn-xs btn-success' onclick='OnCreateReport();'>生成报告</button>";
    document.getElementById("lA").appendChild(div);
}

//初始化筛选栏可选项
for (let i = 0; i < neededOptionIndex.length; i++) {
    for (let j = 0; j < neededOptionIndex[i].length; j++) {
        let option = document.createElement("option");
        option.innerHTML = title[i][neededOptionIndex[i][j]];
        document.getElementById("searcher" + i).children[1].appendChild(option);
    }
}
//for (let i = 0; i < 3; i++) {
//    for (let j = 0; j < methodCount[i]; j++) {
//        let option = document.createElement("option");
//        option.innerHTML = title[i][j];
//        document.getElementById("searcher" + i).children[1].appendChild(option);
//    }
//}
//初始化表格
for (let i = 0; i < 3; i++) {
    let inner = "<td>&nbsp;</td>";
    for (let j = 0; j < title[i].length - 1; j++) {
        inner += "<td />";
    }
    for (let j = 0; j < countPerPage[i]; j++) {
        var tr = document.createElement("tr");
        tr.innerHTML = inner;
        tbody[i].appendChild(tr);
    }
}
//装填标题栏
for (let i = 0; i < 3; i++) {
    for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
        var li = document.createElement("li");
        li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
        var a = document.createElement("a");
        a.innerHTML = j + 1;
        a.setAttribute("href", "#");
        li.appendChild(a);
        pages[i].insertBefore(li, pages[i].lastElementChild);
    }
    for (let j = 0; j < title[i].length; j++) {
        let t = document.createElement("th");
        t.innerHTML = title[i][j];
        table[i].children[0].children[0].appendChild(t);
    }
    OnClick(1, i);
}
// 生成翻页按钮
function OnClick(index, cardIndex) {

    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        // console.log("b" + pageID);
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = 0; i < dummyData[cardIndex].length; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    // console.log("c" + pageID);
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            //显示操作按钮，修改部分字体颜色
            //超级管理员审批报告
            if (authority[18] === 1){
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/viewReport.html?id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                if (tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML == '未审批'){
                    button.innerHTML = '审批'
                }else {
                    button.innerHTML = '查看'
                }
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML
            }else {
                //查看报告（客户）
                if (authority[5] === 1) {
                    let button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewReport.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "查看";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
                }
                //业务查看和发送报告
                else if (authority[6] === 1) {
                    // console.log(dummyData[2])
                    let button = document.createElement("a");
                    // button.setAttribute("class", "btn btn-xs btn-success");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewReport.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "查看";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;
                    console.log(rawData)
                    // console
                    // console.log(rawData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7]=='未发送')
                    // if (rawData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7]=='未发送'){
                        button = document.createElement("a");
                        if(rawData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][7]=='未发送'){
                            // button.setAttribute("class", "");
                            button.setAttribute("href", "javascript:void(0)");
                            button.setAttribute("onclick", "changeState("+
                                rawData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][2] + ")");//pao
                            button.innerHTML = "发送";
                            tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//倒数2
                        }
                        else{
                            button.setAttribute("class", "green");
                            button.setAttribute('href','#' )
                            button.innerHTML = "发送成功！";
                            tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//倒数2
                        }





                }
                //检测主管制定报告
                else if (authority[7] === 1) {
                    if (cardIndex === 0) {
                        let checkbox = document.createElement("input");
                        checkbox.setAttribute("type", "checkbox");
                        checkbox.setAttribute("onclick", "OnCheckboxChange(" + cardIndex + ", " +
                            ((index - 1) * countPerPage[cardIndex] + i) + ", this" + ");");
                        if (chosenWorkpiece.indexOf(dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i]) >= 0) {
                            checkbox.setAttribute("checked", "checked");
                        }
                        tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = checkbox.outerHTML;
                    }
                    else if (cardIndex === 1) {
                        let button = document.createElement("a");
                        button.setAttribute("href", "javascript:void(0)");
                        button.setAttribute("onclick", "window.location.href='/viewReport.html?id="
                            + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                        button.innerHTML = "查看";
                        tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
                    }
                }
                //审核人员审核报告
                else if (authority[8] === 1) {
                    let button = document.createElement("a");
                    button.setAttribute("href", "javascript:void(0)");
                    button.setAttribute("onclick", "window.location.href='/viewReport.html?id="
                        + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                    button.innerHTML = "查看";
                    if (tbody[cardIndex].children[i].children[title[cardIndex].length - 4] == "未审核") button.innerHTML = "审核";
                    tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
                }
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
            for (let j = 1; j < title[cardIndex].length; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnConfirmDepute() {
    window.location.href = "newDepute.html?id=" + projectName[projectOption.selectedIndex][1];
}

// function OnChange(select, index) {
//     switch (select.selectedIndex) {
//         case 1: {
//             window.location.href = "/viewReport.html?id=" + index;
//             break;
//         }
//         case 2: {
//             alert("你选择了发布报告" + index + "！该功能尚未完成");
//             location.reload();
//             break;
//         }
//     }
// }

// function OnChange_1(select, index) {
//     switch (select.selectedIndex) {
//         case 1: {
//             window.location.href = "/viewDepute.html?id=" + index;
//             break;
//         }
//         case 2: {
//             window.location.href = "/viewTechCard.html?id=" + index;
//             break;
//         }
//         case 3: {
//             window.location.href = "/viewReport.html?id=" + index;
//             break;
//         }
//     }
// }

function OnCheckboxChange(cardIndex, tableIndex, box) {
    let id = dummyData[cardIndex][tableIndex];
    if (box.checked) {
        chosenWorkpiece.push(id);
    } else {
        chosenWorkpiece.splice(chosenWorkpiece.indexOf(id), 1);
    }
}

function OnCreateReport() {
    if (chosenWorkpiece.length === 0) {
        alert("你还没有选择工件");
        return;
    }
    else {
        console.log(chosenWorkpiece)
    }
    var ids = [[], [], [], [], []];
    for (let i = 0; i < chosenWorkpiece.length; i++) {
        ids[0].push(chosenWorkpiece[i][0]);
        ids[1].push(chosenWorkpiece[i][15]);
        ids[2].push(chosenWorkpiece[i][18]);
        ids[3].push(chosenWorkpiece[i][16]);
        ids[4].push(chosenWorkpiece[i][17]);
    }
    $.ajax({
        url: '/echo/report',
        type: 'post',
        data: {
            "requestType": "sessionData",
            "pj_id": ids[0],
            "task_ids": ids[1],
            "w_ids": ids[2],
            "op_ids": ids[3],
            "tech_ids": ids[4]
        },
        async: false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                window.location.href = "/createReport.html";
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
}

function OnSearch(cardIndex) {
    let searcher = document.getElementById("searcher" + cardIndex);//搜索栏对象
    let condition = searcher.children[1].selectedIndex;//下拉菜单中选择的那一项的下标
    let details = searcher.children[3].value;//检索的内容
    let timeRange = [searcher.children[5].value, searcher.children[7].value];//两个用来检索的时间，中间必定有一个逗号
    var handyData = rawData[cardIndex];//该页面上的信息
    var resultData = new Array();//存放检索结果
    for (let i = 0; i < handyData.length; i++) {
        //字符串匹配，长度为0则全匹配
        var regx = new RegExp(details);
        //if (regx.exec(handyData[i][condition])) {
        if (regx.exec(handyData[i][neededOptionIndex[cardIndex][condition]])) {
            if (authority[5] === 1) {
                if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][3])
                    continue;
                if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][3])
                    continue;
            }
            else if (authority[6] === 1) {
                if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][6])
                    continue;
                if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][6])
                    continue;
            }
            else if (authority[7] === 1) {
                let tempTimeIndex = 0;
                if (cardIndex === 0)
                    tempTimeIndex = 14;
                else if (cardIndex === 1)
                    tempTimeIndex = 4;
                else if (cardIndex === 2)
                    tempTimeIndex = 6;

                if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][tempTimeIndex])
                    continue;
                if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][tempTimeIndex])
                    continue;
            }
            else if (authority[8] === 1) {
                if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][4])
                    continue;
                if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][4])
                    continue;
            }
            resultData.push(handyData[i]);//加入符合的项
        }
    }
    dummyData[cardIndex] = resultData;//替换，反正再返回当前页面会变回来

    OnClick(1, cardIndex);
}

function changeState(r_id) {
    $.ajax({
        url: '/echo/report',
        type: 'post',
        data:{
            "requestType" : "changeState",
            "r_id":r_id,
        },
        async:false,
        traditional: true,
        success: function (data, status) {
            if (status == 'success') {
                alert("发送成功")
                location.reload();
            }
        },
        error: function (data, status) {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });


}